<?php

declare(strict_types=1);

namespace app;

use think\facade\Filesystem;

class Upload
{
    public function fileImg($request)
    {
        if (!empty($request['img'])) {
            return 1;
            $image = $request['img'];
            $reg = '/data:image\/(\w+?);base64,(.+)$/si';
            preg_match($reg, $image, $match_result);
            $time = time();
            $file_name = md5("s" . $time . 's') . '.' . $match_result[1];
            $logo_path = 'storage/upload/' . date("Ymd") . "/" . $file_name;
            $path = file_put_contents($logo_path, base64_decode($match_result[2]));
        } else {
            $image =  request()->file('img');
            $saveName = Filesystem::disk('public')->putFile('upload', $image, 'data');
            $logo_path = "storage/" . $saveName;
            $file_name = str_replace("upload/" . date("Ymd") . "/", "", $saveName);
        }
        return $logo_path;
    }

}
